<template>
  <div>
    <!--面包屑导航-->
    <Breadcrumb>
      <template #firstTitle>
        <span>商品管理</span>
      </template>
      <template #secondTitle>
        <span>商品列表</span>
      </template>
    </Breadcrumb>

    <!--卡片视图-->
    <el-card>
      <!--搜索与添加-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!--商品列表-->
      <el-table
        border
        stripe
        :data="goodsList">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="95px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="75px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-tooltip class="item" effect="dark" content="修改" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.goods_id)"></el-button>
            </el-tooltip>
            <!--删除按钮-->
            <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteById(scope.row.goods_id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  import Breadcrumb from '@/components/common/Breadcrumb.vue'

  export default {
    name: "GoodsList",
    data(){
      return {
        queryInfo: {  // 获取商品列表的参数对象
          query: '',
          pagenum: 1, // 当前的页数
          pagesize: 5 // 当前每页显示多少条数据
        },
        goodsList: [], //商品列表
        total: 0,
      }
    },
    components: {
      Breadcrumb
    },
    created() {
      this.getGoodsList();
    },
    methods: {
      // 获取商品列表
      async getGoodsList() {
        const {data: res} = await this.$http.get('goods', {params: this.queryInfo});
        // console.log(res);
        if (res.meta.status !== 200) {
          this.$message.error(`获取用户数据: ${res.meta.msg}`);
          return;
        }
        this.goodsList = res.data.goods;
        this.total = res.data.total;
      },
      // 监听pageSize改变
      handleSizeChange(newSize) {
        // console.log(`每页 ${newSize} 条`);
        this.queryInfo.pagesize = newSize;
        this.getGoodsList();
      },
      // 监听页码值的改变
      handleCurrentChange(newPage) {
        // console.log(`当前页: ${newPage}`);
        this.queryInfo.pagenum = newPage;
        this.getGoodsList();
      },
      // 删除提示对话框
      deleteById(id) {
        this.$confirm('此操作将永久删除该商品数据, 是否继续?', '删除商品信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data: res} = await this.$http.delete(`goods/${id}`);
          // console.log(res);
          if (res.meta.status !== 200) {
            this.$message.error('删除失败');
            return;
          }
          this.$message.success('删除成功!');
          this.getGoodsList();
        }).catch(() => {
          this.$message.info('已取消删除');
        });
      },
      // 跳转添加商品页面
      goAddGoodsPage(){
        this.$router.push('/goods/add');
      },
      // 展示修改商品信息对话框
      showEditDialog(id) {
        this.$router.push({path: `/goods/edit/${id}` });
      },
    }
  }
</script>

<style scoped>

</style>
